<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\Order;

class OrderController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $items = Order::orderBy('id', 'desc')->paginate();
        return view('admin.order.index', ['items' => $items]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('admin.order.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $messages = [
            'name.required' => '请输入商品名称',
            'name.max' => '商品名称最长 :max 个字符',
            'type.required' => '请输入类型',
            'type.integer' => '类型必须是正整数',
        ];
        $this->validate($request, [
            'name' => 'required|max:255',
            'type' => 'required|integer',
        ], $messages);

        $item = new Order;
        $item->name = $request->name;
        $item->type = $request->type;
        if ($item->save()) {
            return redirect('/admin/order')->with(['msg' => '创建成功', 'code' => 200]);
        } else {
            return redirect('/admin/order')->with(['msg' => '创建失败', 'code' => 500]);
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
    	$data=Order::findOrFail($id);
        dd($data->toArray());
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $data=Order::findOrFail($id);
        return view('admin.order.edit', ['items' => $data]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
    	$messages = [
            'name.required' => '请输入商品名称',
            'name.max' => '商品名称最长 :max 个字符',
            'type.required' => '请输入类型',
            'type.integer' => '类型必须是正整数',
        ];
        $this->validate($request, [
            'name' => 'required|max:255',
            'type' => 'required|integer',
        ], $messages);

        $item = Order::findOrFail($id);
        $item->name = $request->name;
        $item->type = $request->type;
        if ($item->save()) {
            /*return redirect('/admin/order')->with(['msg' => '修改成功', 'code' => 200]);*/
            return response()->json(['msg' => '修改成功', 'code' => 200]);
        } else {
            // return redirect('/admin/order')->with(['msg' => '修改失败', 'code' => 500]);
            return response()->json(['msg' => '修改失败', 'code' => 500]);
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        if (Order::destroy($id)) {
            return response()->json(['msg' => '删除成功', 'code' => 200]);
        } else {
            return response()->json(['msg' => '删除失败', 'code' => 500]);
        }
    }
}
